using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEditor;
using SgsUI;

namespace SgsUI.Editor
{
    public class ViewEditorBase : UnityEditor.Editor
    {
        private List<string> base_prop_key_list = new List<string>
        {
            "m_Script","id","type","align","z","anchor_p","anchor","pos","size","rotation","scale","visible"
        };

        private Dictionary<string,SerializedProperty> base_prop_map = new Dictionary<string, SerializedProperty>();

        private ViewNode m_viewNode;

        protected virtual void OnEnable()
        {
            foreach(string key in base_prop_key_list)
            {
                base_prop_map[key] = serializedObject.FindProperty(key);
            }

            m_viewNode = target as ViewNode;
        }

        public override void OnInspectorGUI()
        {

            m_viewNode.UpdateRectTransform();

            serializedObject.ApplyModifiedProperties();
 
            serializedObject.Update();
            GUI.enabled = false;

            LayoutBaseParam();
        }


        public void LayoutBaseParam()
        {
            EditorGUILayout.PropertyField(base_prop_map["type"]);

            if(base_prop_map["id"].stringValue != String.Empty)
                EditorGUILayout.PropertyField(base_prop_map["id"]);

            if(base_prop_map["anchor"].stringValue != String.Empty && base_prop_map["pos"].vector2Value != Vector2.zero)
            {
                EditorGUILayout.PropertyField(base_prop_map["align"]);
            }
            else if(base_prop_map["anchor"].stringValue != String.Empty)
            {
                EditorGUILayout.PropertyField(base_prop_map["anchor"]);
            }
            else if(base_prop_map["anchor_p"].vector2Value != Vector2.zero)
            {
                EditorGUILayout.PropertyField(base_prop_map["anchor_p"]);
                if(base_prop_map["pos"].vector2Value != Vector2.zero)
                {
                    EditorGUILayout.PropertyField(base_prop_map["pos"]);
                }
            }
            else
            {
                if(base_prop_map["pos"].vector2Value != Vector2.zero)
                {
                    EditorGUILayout.PropertyField(base_prop_map["pos"]);
                }
            }


            if(m_viewNode.HasAtriSize())
            {
                if(base_prop_map["size"].vector2Value != Vector2.zero)
                {
                    EditorGUILayout.PropertyField(base_prop_map["size"]);
                }
            }

            if(base_prop_map["z"].intValue != 0)
            {
                EditorGUILayout.PropertyField(base_prop_map["z"]);
            }

            if(base_prop_map["rotation"].floatValue != 0.0f)
            {
                EditorGUILayout.PropertyField(base_prop_map["rotation"]);
            }

            if(base_prop_map["scale"].vector2Value != Vector2.one)
            {
                EditorGUILayout.PropertyField(base_prop_map["scale"]);
            }

            if(!base_prop_map["visible"].boolValue)
            {
                EditorGUILayout.PropertyField(base_prop_map["visible"]);
            }
        }
    }
}